package com.example.forjava.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Order(Integer.MIN_VALUE)
public class LogAdvice {

    // 定义一个切点：所有被GetMapping注解修饰的方法会织入advice
    @Pointcut("@annotation(org.springframework.web.bind.annotation.GetMapping)")
    private void logAdvicePointcut() {}

    // Before表示logAdvice将在目标方法执行前执行
    @Before("logAdvicePointcut()")
    public void logAdviceBefore(){
        System.out.println("getBefore请求的advice触发了");
    }


    // After表示logAdvice将在目标方法执行后执行
    @After("logAdvicePointcut()")
    public void logAdviceAfter(){
        System.out.println("getAfter请求的advice触发了");
    }


    @Around("logAdvicePointcut()")
    public Object logAdviceAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("getAround请求的advice触发了");
        return proceedingJoinPoint.proceed();
    }


    @AfterReturning("logAdvicePointcut()")
    public void logAdviceAfterReturning(){
        System.out.println("getAfterReturning请求的advice触发了");
    }
}
